﻿using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 大屏图片
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司
    /// 作 者：管理员/admin
    /// 日 期：2020-08-03 11:49
    /// </summary>
    [Route("api/DataV/[controller]")]
    public class ImagesController : BaseApiController
    {
        #region Get
        /// <summary>
        /// 获取图片列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("{type}")]
        [AllowAnonymous]
        public ResponseResult GetList(string type)
        {
            var filePath = ConfigurationKey.BiVisualFilePath + type + "/";
            var fileData = FileHelper.GetAllFiles(filePath);
            var output = fileData.Select(f => new { fileName = f.Name }).ToList();
            return Success(output);
        }

        /// <summary>
        /// 查看图片
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        [HttpGet("{type}/{fileName}")]
        [AllowAnonymous]
        public FileContentResult GetInfo(string type, string fileName)
        {
            var filePath = ConfigurationKey.BiVisualFilePath + type + "/";
            var fileData = FileHelper.GetAllFiles(filePath);
            var output = fileData.Find(f => f.Name == fileName);
            var imgStream = new MemoryStream();
            if (output != null)
            {
                var imgByte = FileHelper.ReadAllBytes(output.FullName);
                if (imgByte.IsNotEmptyOrNull())
                {
                    imgStream = new MemoryStream(imgByte);
                }
                return File(imgStream.ToArray(), @"image/" + output.Extension);
            }
            return null;
        }
        #endregion

        #region Post
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost("{type}/Uploader")]
        [AllowAnonymous]
        public ResponseResult UploadImage(string type)
        {
            var file = Utils.HttpContext.Current.Request.Form.Files[0];
            var ImgType = Path.GetExtension(file.FileName).Replace(".", "");
            if (!this.AllowImageType(ImgType))
            {
                return Error("上传失败，图片格式不允许上传");
            }
            var filePath = ConfigurationKey.BiVisualFilePath + "/" + type + "/";
            var fileName = DateTime.Now.ToString("yyyyMMdd") + "_" + CommonHelper.Guid.ToString() + Path.GetExtension(file.FileName);
            using (var stream = new FileStream(filePath + fileName, FileMode.Create))
            {
                file.CopyTo(stream);
            }
            return Success("上传成功", new { fileName = fileName });
        }
        #endregion

        #region Method
        /// <summary>
        /// 允许文件类型
        /// </summary>
        /// <param name="fileExtension">文件后缀名</param>
        /// <returns></returns>
        private bool AllowImageType(string fileExtension)
        {
            var isOk = false;
            var allowExtension = ConfigurationKey.AllowUploadImageType;
            foreach (var item in allowExtension)
            {
                if (item == fileExtension.ToLower())
                {
                    isOk = true;
                    break;
                }
                else
                {
                    isOk = false;
                }
            }
            return isOk;
        }
        #endregion
    }
}
